package com.example.multiactivitydemo

import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class FourthActivity : AppCompatActivity() {

    private lateinit var tvTimer: TextView
    private lateinit var btnStart: Button
    private lateinit var btnPause: Button
    private lateinit var btnReset: Button

    private var isRunning = false
    private var startTime = 0L
    private var elapsedTime = 0L

    private val handler = Handler()
    private lateinit var runnable: Runnable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)

        tvTimer = findViewById(R.id.tvTimer)
        btnStart = findViewById(R.id.btnStart)
        btnPause = findViewById(R.id.btnPause)
        btnReset = findViewById(R.id.btnReset)

        setupButtons()

        runnable = Runnable {
            if (isRunning) {
                val currentTime = System.currentTimeMillis()
                elapsedTime = currentTime - startTime
                updateTimerText(elapsedTime)
                handler.postDelayed(runnable, 10)
            }
        }

        // 恢复状态
        if (savedInstanceState != null) {
            isRunning = savedInstanceState.getBoolean(KEY_IS_RUNNING, false)
            startTime = savedInstanceState.getLong(KEY_START_TIME, 0)
            elapsedTime = savedInstanceState.getLong(KEY_ELAPSED_TIME, 0)

            if (isRunning) {
                startTime = System.currentTimeMillis() - elapsedTime
                startTimer()
            } else {
                updateTimerText(elapsedTime)
            }
        }
        updateButtonStates()
    }

    private fun setupButtons() {
        btnStart.setOnClickListener {
            if (!isRunning) {
                startTime = System.currentTimeMillis() - elapsedTime
                startTimer()
                isRunning = true
                updateButtonStates()
            }
        }

        btnPause.setOnClickListener {
            if (isRunning) {
                stopTimer()
                elapsedTime = System.currentTimeMillis() - startTime
                isRunning = false
                updateButtonStates()
            }
        }

        btnReset.setOnClickListener {
            stopTimer()
            elapsedTime = 0
            isRunning = false
            updateTimerText(0)
            updateButtonStates()
        }
    }

    private fun startTimer() {
        handler.postDelayed(runnable, 10)
    }

    private fun stopTimer() {
        handler.removeCallbacks(runnable)
    }

    private fun updateTimerText(timeInMillis: Long) {
        val hours = timeInMillis / 3600000
        val minutes = (timeInMillis % 3600000) / 60000
        val seconds = (timeInMillis % 60000) / 1000
        val milliseconds = (timeInMillis % 1000) / 10

        val timeString = String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, milliseconds)
        tvTimer.text = timeString
    }

    private fun updateButtonStates() {
        btnStart.isEnabled = !isRunning
        btnPause.isEnabled = isRunning
        btnReset.isEnabled = !isRunning && elapsedTime > 0
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean(KEY_IS_RUNNING, isRunning)
        outState.putLong(KEY_START_TIME, startTime)
        outState.putLong(KEY_ELAPSED_TIME, elapsedTime)
    }

    override fun onDestroy() {
        super.onDestroy()
        stopTimer()
    }

    companion object {
        private const val KEY_IS_RUNNING = "is_running"
        private const val KEY_START_TIME = "start_time"
        private const val KEY_ELAPSED_TIME = "elapsed_time"
    }
}